﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace XNADota.Input
{
    public class InputState : XNADotaObject
    {
        private const float Margin = 10;

        private KeyboardState _CurrentKeyboardState = Keyboard.GetState();
        private MouseState _CurrentMouseState = Mouse.GetState();

        private KeyboardState _LastKeyboardState = Keyboard.GetState();
        private MouseState _LastMouseState = Mouse.GetState();

        private Rectangle _SelectRectangle = Rectangle.Empty;

        public InputState(Game game)
            : base(game)
        {
        }

        public void Update(GameTime gameTime)
        {
            _LastKeyboardState = _CurrentKeyboardState;
            _LastMouseState = _CurrentMouseState;

            _CurrentKeyboardState = Keyboard.GetState();
            _CurrentMouseState = Mouse.GetState();
        }

        public void Update()
        {
            if (LeftButtonSingleClick)
            {
                _SelectRectangle.X = (int)CursorPostion.X;
                _SelectRectangle.Y = (int)CursorPostion.Y;
                _SelectRectangle.Width = 0;
                _SelectRectangle.Height = 0;
            }
            if (LeftButtonAlwaysPressed || LeftButtonJustRelease)
            {
                _SelectRectangle.Width = (int)(CursorPostion.X - _SelectRectangle.X);
                _SelectRectangle.Height = (int)(CursorPostion.Y - _SelectRectangle.Y);
            }
            Update(new GameTime());
        }

        #region Mouse input
        public Rectangle SelectRectangle
        {
            get { if (_SelectRectangle.Width > 0 && _SelectRectangle.Height > 0) return _SelectRectangle; else return Rectangle.Empty; }
        }

        public bool LeftButtonAlwaysPressed
        {
            get { return _CurrentMouseState.LeftButton == ButtonState.Pressed && _LastMouseState.LeftButton == ButtonState.Pressed; }
        }

        public bool LeftButtonJustRelease
        {
            get { return _CurrentMouseState.LeftButton == ButtonState.Released && _LastMouseState.LeftButton == ButtonState.Pressed; }
        }

        public bool LeftButtonSingleClick
        {
            get { return _CurrentMouseState.LeftButton == ButtonState.Pressed && _LastMouseState.LeftButton == ButtonState.Released; }
        }

        public bool LeftButtonDoubleClick
        {
            get { return _CurrentMouseState.LeftButton == ButtonState.Pressed && _LastMouseState.LeftButton == ButtonState.Pressed; }
        }

        public bool RightButtonSingleClick
        {
            get { return _CurrentMouseState.RightButton == ButtonState.Pressed && _LastMouseState.RightButton == ButtonState.Released; }
        }

        public bool RightButtonDoubleClick
        {
            get { return _CurrentMouseState.RightButton == ButtonState.Pressed && _LastMouseState.RightButton == ButtonState.Pressed; }
        }

        public float MouseDelta
        {
            get { return _CurrentMouseState.ScrollWheelValue - _LastMouseState.ScrollWheelValue; }
        }

        public bool IsOnLeftMargin
        {
            get { return _CurrentMouseState.X < Margin; }
        }

        public bool IsOnRightMargin
        {
            get { return _CurrentMouseState.X > Game.GraphicsDevice.Viewport.Width - Margin; }
        }

        public bool IsOnTopMargin
        {
            get { return _CurrentMouseState.Y < Margin; }
        }

        public bool IsOnBottomMargin
        {
            get { return _CurrentMouseState.Y > Game.GraphicsDevice.Viewport.Height - Margin; }
        }

        public bool IsLeftButtonClick
        {
            get { return _CurrentMouseState.LeftButton == ButtonState.Pressed && _LastMouseState.LeftButton == ButtonState.Released; }
        }

        public Vector2 CursorPostion
        {
            get { return new Vector2(_CurrentMouseState.X, _CurrentMouseState.Y); }
        }
        #endregion

        #region Keyboard input
        public bool IsKeyJustDown(Keys key)
        {
            return _CurrentKeyboardState.IsKeyDown(key) && _LastKeyboardState.IsKeyUp(key);
        }

        public bool IsKeyDown(Keys key)
        {
            return _CurrentKeyboardState.IsKeyDown(key);
        }
        #endregion
    }
}
